import requests
from urllib.parse import unquote
import  re
from  somecookie import readCookie,writeCookies


def getUrl():
    #不变       'ALC': 'ac%3D2%26bt%3D1661653264%26cv%3D5.0%26et%3D1693189264%26ic%3D2028368508%26login_time%3D1661538154%26scf%3DArIp6vHIiPeIObG3rpSLWIwZwiJhoYxOoGu_SiMgLsUeBorbeEbYZ1p0tbcoOZd9aWvzdlYL4YPXs6ajn6aaKn0.%26uid%3D7575693963%26vf%3D0%26vs%3D1%26vt%3D0%26es%3Dff1d7f6055370c6c52b080839105c78a',
    #不变       'ALF': '1693189264',
    #同1        'SUB': '_2A25ODqUwDeRhGeFL7FcX-S3FzT-IHXVtfZH4rDV_PUJbm9ANLRbVkW9NfaRKfCvVjUu4xYN3DQ9EgewWQtIykqxi',
    #同1        'LT': '1661654368',
    # set-cookie: SUB=_2A25ODzndDeRhGeFL7FcX-S3FzT-IHXVtfSwVrDV_PUJbm9ANLXL5kW9NfaRKfBYFzisYR44oIPBkNMMg5AmqlETF; path=/; domain=.sina.com.cn; secure; httponly; SameSite=None
    # set-cookie: SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW2_Qf-F90b4_Qad-Wh4Vbv5NHD95QNSKMfSo.01Kq0Ws4Dqcj_i--fi-2Xi-2Ni--Xi-i8i-27i--NiKn4iKLFi--ciKLhiKn4i--fi-i8i-zN; path=/; domain=.sina.com.cn; secure; SameSite=None
    # set-cookie: LT=1661684109; path=/; domain=login.sina.com.cn

    cookies = {
        'ALC': readCookie('ALC'),
        'ALF': readCookie('ALF'),
        'SUB':readCookie('SUB1') ,
        'LT': readCookie('LT'),
    }

    headers = {
        'authority': 'login.sina.com.cn',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        # Requests sorts cookies= alphabetically
        # 'cookie': 'SINAGLOBAL=180.91.164.12_1627735608.820103; Qs_lvt_335601=1637577947; Qs_pv_335601=2456151311353380400%2C1574876041120961800; SCF=AvBXETlJ-dfVEJ3UJtBMxHTn6JDcvInUEbB89dp7htJI7ifmILVXfv_H97hEj534RWalyERU543zfLniXGOQo6k.; UOR=www.baidu.com,news.sina.com.cn,; ULV=1659360855571:1:1:1::; U_TRS1=0000004c.48b76cf9.62e7d659.0a00fda1; Hm_lvt_b82ffdf7cbc70caaacee097b04128ac1=1659360857,1660736087; SUB=_2A25OA-O3DeRhGeFL7FcX-S3FzT-IHXVtD43_rDV_PUNbm9ANLRLBkW1NfaRKfFe7XP-gt51h-qL2zUYDUkokaFBY; ALC=ac%3D2%26bt%3D1661440999%26cv%3D5.0%26et%3D1692976999%26ic%3D-1221144719%26login_time%3D1661440999%26scf%3D%26uid%3D7575693963%26vf%3D0%26vs%3D1%26vt%3D0%26es%3D084105e62df4f355bddff008b754426a; ALF=1692976999',
        # 'cookie':'SINAGLOBAL=180.91.164.12_1627735608.820103; Qs_lvt_335601=1637577947; Qs_pv_335601=2456151311353380400%2C1574876041120961800; SCF=AvBXETlJ-dfVEJ3UJtBMxHTn6JDcvInUEbB89dp7htJI7ifmILVXfv_H97hEj534RWalyERU543zfLniXGOQo6k.; UOR=www.baidu.com,news.sina.com.cn,; ULV=1659360855571:1:1:1::; U_TRS1=0000004c.48b76cf9.62e7d659.0a00fda1; Hm_lvt_b82ffdf7cbc70caaacee097b04128ac1=1659360857,1660736087; tgc=TGT-NzU3NTY5Mzk2Mw==-1661674564-yf-452E655B2263222B9DDE7BBB321A5175-1; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW2_Qf-F90b4_Qad-Wh4Vbv5NHD95QNSKMfSo.01Kq0Ws4Dqcj_i--fi-2Xi-2Ni--Xi-i8i-27i--NiKn4iKLFi--ciKLhiKn4i--fi-i8i-zN; ALC=ac%3D27%26bt%3D1661674565%26cv%3D5.0%26et%3D1693210565%26ic%3D1862831876%26login_time%3D1661440999%26scf%3DAvBXETlJ-dfVEJ3UJtBMxHTn6JDcvInUEbB89dp7htJI7ifmILVXfv_H97hEj534RWalyERU543zfLniXGOQo6k.%26uid%3D7575693963%26vf%3D0%26vs%3D1%26vt%3D0%26es%3Df8cb1c09980e394bf35ced5bedcb7b45; ALF=1693210565; SUB=_2A25ODzfoDeRhGeFL7FcX-S3FzT-IHXVtfS4grDV_PUJbm9ANLVGhkW9NfaRKfJi0WyhUDBOpNLDx4TALjq_nowpI; LT=1661683640; U_TRS2=0000003d.19dcd8cf.630b47b9.a2f2c892; Apache=101.106.20.101_1661683663.951463',
        'pragma': 'no-cache',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
    }

    params = {
        'url': 'https://weibo.com/',
        '_rand': '1661674564.6584',
        'gateway': '1',
        'service': 'miniblog',
        'entry': 'miniblog',
        'useticket': '1',
        'returntype': 'META',
        '_client_version': '0.6.36',
    }
    url = 'https://login.sina.com.cn/sso/login.php'
    r = requests.get(url, params=params, cookies=cookies, headers=headers, allow_redirects=False)

    r.encoding = r.apparent_encoding
    SUB = r.cookies._find('SUB')
    LT  = r.cookies._find('LT')
    writeCookies('SUB1',SUB)
    writeCookies('LT',LT)
    # print(r.text)
    # print(r.cookies)
    res =re.search('location.replace\((.*?)\)',r.text).groups()[0]
    url =re.search('miniblog&r=(.*)',unquote(res)).groups()[0]
    return url


# https://login.sina.com.cn/crossdomain2.php?action=login&entry=miniblog&r=https%3A%2F%2Fpassport.weibo.com%2Fwbsso%2Flogin%3Fssosavestate%3D1693220598%26url%3Dhttps%253A%252F%252Fweibo.com%252F%26display%3D0%26ticket%3DST-NzU3NTY5Mzk2Mw%3D%3D-1661684598-yf-4BD929FE70B5767CACDF24C777762FD4-1%26retcode%3D0&login_time=1661440999&sign=d4d42350c83ff90e
# <RequestsCookieJar[<Cookie ALC=ac%3D27%26bt%3D1661684598%26cv%3D5.0%26et%3D1693220598%26ic%3D1862831876%26login_time%3D1661440999%26scf%3D%26uid%3D7575693963%26vf%3D0%26vs%3D1%26vt%3D0%26es%3D5b737b6298415e6f8d819690cf20eacc for .login.sina.com.cn/>,
#  <Cookie LT=1661684598 for .login.sina.com.cn/>,
#  <Cookie tgc=TGT-NzU3NTY5Mzk2Mw==-1661684598-yf-DA3E80795FB148E90AD32F3197FDF1A3-1 for .login.sina.com.cn/>,
#  <Cookie ALF=1693220598 for .sina.com.cn/>, <Cookie SCF=AiiQ-2gqVBs5Tp-IK08shySVH2QTSDOCqHvjetjIHJBwzLm55adHvj_fLeNqycLHzwTrPa91U5T9WIyvIPg5bdQ. for .sina.com.cn/>,
#  <Cookie SUB=_2A25ODzsmDeRhGeFL7FcX-S3FzT-IHXVtfSvurDV_PUNbm9ANLU_gkW9NfaRKfBuk4UMl3ivF3sd8PmOP4a3_P0IC for .sina.com.cn/>, <Cookie SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW2_Qf-F90b4_Qad-Wh4Vbv5NHD95QNSKMfSo.01Kq0Ws4Dqcj_i--fi-2Xi-2Ni--Xi-i8i-27i--NiKn4iKLFi--ciKLhiKn4i--fi-i8i-zN for .sina.com.cn/>]>



def changeCooie(url):
    # set-cookie: SCF=AvBXETlJ-dfVEJ3UJtBMxHTn6JDcvInUEbB89dp7htJIthsNbPgB9gWhIxYHZgji-FMgM2VgBa2pfibiPLm3P8E.; expires=Wednesday, 25-Aug-2032 08:16:08 GMT; path=/; domain=.weibo.com; secure; httponly; SameSite=None
    # set-cookie: SUB=_2A25OD1QYDeRhGeFL7FcX-S3FzT-IHXVtfcLQrDV8PUNbmtANLVftkW9NfaRKfIEKeLKKR5W2EzlIylLQUiAJSW3s; path=/; domain=.weibo.com; secure; httponly; SameSite=None
    # set-cookie: SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW2_Qf-F90b4_Qad-Wh4Vbv5JpX5KMhUgL.FoMfS0-c1Ke4Soe2dJLoIEXLxK-LBKBLBKMLxKBLB.-LBK5LxKML1h.L1-zLxKqL1-eL1h.LxK-LB.-LBoMt; expires=Monday, 28-Aug-2023 08:16:08 GMT; path=/; domain=.weibo.com; secure; SameSite=None
    # set-cookie: SUHB=deleted; expires=Thursday, 01-Jan-1970 00:00:01 GMT; path=/; domain=.weibo.com; secure; SameSite=None
    # set-cookie: SRT=D.QqHBJZPLMDEMd4Mb4cYGS4SJirSZdCsOJrSwPCsHNEYdWFSZOEEpMERt4EPKRcsrA4uJPcSrTsVuOcSY4!kcN4PIS4oINszsPqHE5coH5doJ4PP9A49OPqiq*B.vAflW-P9Rc0lR-ykcDvnJqiQVbiRVPBtS!r3JZPQVqbgVdWiMZ4siOzu4DbmKPWQJbyMS8sFKOB8iEyMRdAkP-XrPcH-i49ndDPIOdYPSrnlMcyiSbJnIdJoN-4r4-ybJcM1OFyHMPYJ5mjkOmHIA!oCNrsJ5mkCOmzlI4oCNqPJ5mkiODEfI!noIQ9J5mkoODmkS4noTCuJ5mjkOmzlI4oCUrsr; expires=Wednesday, 25-Aug-2032 08:16:08 GMT; path=/; domain=.passport.weibo.com; secure; httponly; SameSite=None
    # set-cookie: SRF=1661674568; expires=Wednesday, 25-Aug-2032 08:16:08 GMT; path=/; domain=.passport.weibo.com; secure; SameSite=None
    # set-cookie: ALF=1693210565; expires=Monday, 28-Aug-2023 08:16:05 GMT; path=/; domain=.weibo.com; secure; SameSite=None

    # 不变 'ALF': '1693189264',
    # 不变 'SSOLoginState': '1661653265',
    # 1setcookie    'SUBP': '0033WrSXqPxfM725Ws9jqgMF55529P9D9WW2_Qf-F90b4_Qad-Wh4Vbv5JpX5K-hUgL.FoMfS0-c1Ke4Soe2dJLoIEXLxK-LBKBLBKMLxKBLB.-LBK5LxKML1h.L1-zLxKqL1-eL1h.LxK-LB.-LBoMt',
    # 1setcookie   'SCF': 'ArIp6vHIiPeIObG3rpSLWIwZwiJhoYxOoGu_SiMgLsUeEsjpxQxEvSpE2_3V5lH92BNby08DVs46SKoLsC83Q9c.',
    # 1setcookie  'SUB': '_2A25ODqUwDeRhGeFL7FcX-S3FzT-IHXVtfZH4rDV8PUJbmtANLXPGkW9NfaRKfFELo3KJFHJfN5hOGCuDKPpqNiY4',
    # 1setcookie   'SRT': 'D.QqHBJZPLMGEPWrMb4cYGS4SJirSZdCsOJrSwPCsHNEYdWFSTNDMpMERt4EP1RcsrA4uJdEBGTsVuOcSY4!kcM!PJUqiIN!SzNcSKidYLMrisMmkAVGEKTP!r*B.vAflW-P9Rc0lR-ykcDvnJqiQVbiRVPBtS!r3JZPQVqbgVdWiMZ4siOzu4DbmKPWQJbyMS8sFKOB8iEyMRdAkP-XrPcH-i49ndDPIIdYPSrnlMcyiSbJnIdJoN-4r4-ybJcM1OFyHMPYJ5mjkOmHIA!oCNrsJ5mkCOmzlI4oCNqPJ5mkiODEfI!noIQ9J5mkoODmkS4noTCuJ5mjkOmzlI4oCUrsr',
    # 1setcookie   'SRF': '1661654368',

    session = requests.session()
    session.headers.clear()
    headers = {
        'pragma': 'no-cache',
        'cache-control': 'no-cache',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'sec-fetch-site': 'cross-site',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-dest': 'document',
        'referer': 'https://login.sina.com.cn/',
        'accept-encoding':'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cookie':'ALF=1693210565;SSOLoginState=1661674567;SUBP='+readCookie('SUBP')+'SUB='+readCookie('SUB')+';SRT='+readCookie('SRT')+';SRF='+readCookie('SRF')+';'

    }
    session.headers.update(headers)
    # url = 'https://passport.weibo.com/wbsso/login?ssosavestate=1693210565&url=https%3A%2F%2Fweibo.com%2F&display=0&ticket=ST-NzU3NTY5Mzk2Mw==-1661674565-yf-C02C2BAB8B9E36F3915C4AB6DEA65B5E-1&retcode=0'
    # url = 'https://passport.weibo.com/wbsso/login?ssosavestate=1693215976&url=https%3A%2F%2Fweibo.com%2F&display=0&ticket=ST-NzU3NTY5Mzk2Mw==-1661679976-yf-02886ABAB29461A723C71CA91F3EC19C-1&retcode=0&login_time=1661440999&sign=3f332ed1d06d4042'

    r = session.get(url,allow_redirects =False)
    r.raise_for_status()
    r.encoding =r.apparent_encoding
    ke=['SUBP','SUB','SRT','SRF']
    for k in ke:
        writeCookies(k,r.cookies._find(k))
    print("更新成功")

def main():
    url = getUrl()
    changeCooie(url)
main()






